package com.lanren.familybookapp.ui.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lanren.common.interfaces.AdapterItemClickView;
import com.lanren.common.utils.LogUtils;
import com.lanren.familybookapp.R;
import com.lanren.familybookapp.app.AppUserHelper;
import com.lanren.familybookapp.ui.home.bean.BookPayTypeBean;

import java.util.List;

/**
 * @ClassName ManagerAdapter
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/10/12 10:53
 * @Version 1.0
 */
public class BookPayTypeAdapter extends RecyclerView.Adapter<BookPayTypeAdapter.ContentHolder> {

    private Context mContext;
    private List<BookPayTypeBean> list;
    private LayoutInflater mLayoutInflater;
    private AdapterItemClickView adapterItemClickView;


    public BookPayTypeAdapter(Context mContext, List<BookPayTypeBean> list) {
        this.mContext = mContext;
        this.list = list;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setAdapterItemClickView(AdapterItemClickView adapterItemClickView) {
        this.adapterItemClickView = adapterItemClickView;
    }

    @NonNull
    @Override
    public ContentHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ContentHolder(mLayoutInflater.inflate(R.layout.item_book_member, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ContentHolder contentHolder, final int i) {

        contentHolder.bookMemberName.setText(list.get(i).getPayType());
        if (AppUserHelper.getInstance().getUser().getUserId().equals(list.get(i).getBookCreateUser())) {
            contentHolder.bookMemberDele.setVisibility(View.VISIBLE);
        } else {
            contentHolder.bookMemberDele.setVisibility(View.GONE);
        }

        contentHolder.bookMemberDele.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adapterItemClickView != null) {
                    adapterItemClickView.onItemClick(i);
                }
            }
        });

        contentHolder.bookItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (adapterItemClickView != null) {
                    adapterItemClickView.onItemClick(i, v);
                }
            }
        });


    }

    public void remove(int position) {
        list.remove(position);
        //删除动画
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ContentHolder extends RecyclerView.ViewHolder {

        TextView bookMemberName;
        ImageView bookMemberDele;
        RelativeLayout bookItemView;

        public ContentHolder(@NonNull View itemView) {
            super(itemView);
            bookMemberName = itemView.findViewById(R.id.book_member_name);
            bookMemberDele = itemView.findViewById(R.id.book_member_dele);
            bookItemView = itemView.findViewById(R.id.book_item_view);
        }
    }
}
